#!/usr/bin/perl 
# (c) Ruslan Mikhaylenko mirusnet@gmail.com 09.2011
#
#
use strict;
use warnings;


my $c = @ARGV;
my $name;

if ( $c != 1 ) {
	printf("%s\n", "Usage: $0 username");
	exit 1;
} else {
	$name = $ARGV[0];
}


my $confdir = "/home/mirus/svn";
chdir $confdir or die "Cannot chdir to $confdir: $!";


my $passwd	= $confdir . '/passwd';
my $auth	= $confdir . '/auth';


unless( -e $passwd ) {
	die "Cannot find file $passwd: $!";
}
unless (-e $auth) {
	die "Cannot find file $auth: $!";
}


# Checking if user already exists on the passwd file
open(PASSWD, $passwd) or die "Can not open file $passwd: $!";
while(my $line = <PASSWD>) {
	chomp($line);
	if ($line=~/$name/) {
		printf("*** ERROR ***\n");
		printf("USER: \"%s\" already exists!\n", $name);
		printf("%s\n", $line);
		close(PASSWD);
		exit(1);
	}
}
close(PASSWD);


# Adding user to the passwd file
open(PASSWD, ">>$passwd") or die "Can not open file $passwd: $!";
printf(PASSWD "%s = svn_%s\n", $name, $name);
printf("USER: \"%s\" successfully added to the passwd file.\n\n\n", $name);
close(PASSWD);


# Adding user to the auth file
open(AUTH, ">>$auth") or die "Can not open file $auth: $!";
	while(my $line = <AUTH>) {
		chomp($line);
		if ($line=~/(^some = some,username,)/) {
			$line .= ",$name";
			printf("%s\n", $line);
		} else {
			print("%s\n", $line);
			}
	}
close(AUTH);




